<template>
  <div class="orders-page">
    <div class="page-header">
      <h1>我的订单</h1>
    </div>
    
    <div class="orders-container">
      <div v-if="orders.length === 0" class="empty-orders">
        <p>暂无订单记录</p>
        <router-link :to="{ name: 'ProductList' }" class="shop-link">
          去购物
        </router-link>
      </div>
      
      <div v-else class="orders-list">
        <div v-for="order in orders" :key="order.id" class="order-card">
          <div class="order-header">
            <div class="order-info">
              <span class="order-id">订单号: {{ order.id }}</span>
              <span class="order-date">{{ formatDate(order.date) }}</span>
            </div>
            <div class="order-total">
              总计: <span class="amount">¥{{ order.total }}</span>
            </div>
          </div>
          
          <div class="order-items">
            <div v-for="item in order.items" :key="item.id" class="order-item">
              <img :src="item.image" :alt="item.name" class="item-image" />
              <div class="item-details">
                <h4>{{ item.name }}</h4>
                <p class="item-price">¥{{ item.price }} × {{ item.quantity }}</p>
              </div>
              <div class="item-subtotal">
                ¥{{ item.price * item.quantity }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useCartStore } from '../stores/cart'

const cartStore = useCartStore()
const orders = computed(() => cartStore.orders)

const formatDate = (dateString) => {
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}
</script>

<style scoped>
.orders-page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.page-header {
  text-align: center;
  margin-bottom: 30px;
}

.page-header h1 {
  color: #333;
  font-size: 28px;
}

.empty-orders {
  text-align: center;
  padding: 60px 20px;
  color: #666;
}

.shop-link {
  display: inline-block;
  margin-top: 20px;
  padding: 10px 20px;
  background: #3498db;
  color: white;
  text-decoration: none;
  border-radius: 4px;
}

.orders-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.order-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.order-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  background: #f8f9fa;
  border-bottom: 1px solid #eee;
}

.order-info {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.order-id {
  font-weight: bold;
  color: #333;
}

.order-date {
  color: #666;
  font-size: 14px;
}

.order-total {
  font-size: 18px;
  color: #333;
}

.amount {
  color: #e74c3c;
  font-weight: bold;
}

.order-items {
  padding: 0;
}

.order-item {
  display: flex;
  align-items: center;
  padding: 15px 20px;
  border-bottom: 1px solid #eee;
}

.order-item:last-child {
  border-bottom: none;
}

.item-image {
  width: 60px;
  height: 60px;
  object-fit: cover;
  border-radius: 4px;
  margin-right: 15px;
}

.item-details {
  flex: 1;
}

.item-details h4 {
  margin: 0 0 5px 0;
  font-size: 16px;
}

.item-price {
  color: #666;
  margin: 0;
}

.item-subtotal {
  font-weight: bold;
  color: #e74c3c;
}
</style>